//
//  HMSlideMenu.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/8.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMSlideMenu.h"
#import "SlideMenuCell.h"
#import "MyInfoModel.h"

@interface HMSlideMenu () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *right;

@property (nonatomic, strong) MyInfoModel * myInfo;

@end

@implementation HMSlideMenu

+ (instancetype)slideMenu
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HMSlideMenu class]) owner:nil options:nil] lastObject];
}

+ (instancetype)showInSuperView:(UIView *)view
{
    HMSlideMenu * menu = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HMSlideMenu class]) owner:nil options:nil] lastObject];
    
    menu.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    
    // 添加平移动画
    menu.transform = CGAffineTransformMakeTranslation(-menu.width, 0);
    [UIView animateWithDuration:0.1 delay:0 usingSpringWithDamping:0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        menu.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
       
    }];
    
    [view addSubview:menu];
    
    return menu;
}

- (void)hide
{
    [self removeFromSuperview];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SlideMenuCell class]) bundle:nil] forCellReuseIdentifier:@"SlideMenuCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.right.constant = KWidthSCREEN / 2;
    [self refreshData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:loginNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:loginExit object:nil];
}

- (void)refreshData
{
    NSDictionary *param =@{@"tel":[MGAccountTool getTel]};
    
    [HttpBaseTool get:kMyinfoUrl params:param success:^(id responseObj) {
        self.myInfo = [MyInfoModel mj_objectWithKeyValues:responseObj[@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [HMHUDManager showHUDWithText:NetWorkError];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SlideMenuCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SlideMenuCell"];
    [cell fillDataWithIndex:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(HMSlideMenu:clickIndex:)]) {
        [_delegate HMSlideMenu:self clickIndex:indexPath];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor colorWithHexString:@"22b76d"];
    
    UIImageView * iconView = [[UIImageView alloc] init];
    iconView.userInteractionEnabled = YES;
    CGFloat iconWH = 60;
    CGFloat iconX = (KWidthSCREEN * 1/2 - iconWH) / 2;
    CGFloat iconY = 47;
    iconView.frame = CGRectMake(iconX, iconY, iconWH, iconWH);
    [iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kImageUrl,_myInfo.photo]] placeholderImage:[UIImage imageNamed:@"home_headportrait"]];
    iconView.layer.cornerRadius = 30;
    iconView.layer.masksToBounds = YES;
    iconView.layer.borderWidth = 2;
    iconView.layer.borderColor = kBackGolor.CGColor;
    [headView addSubview:iconView];
    self.iconView = iconView;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
    tap.numberOfTapsRequired = 1;
    [tap addTarget:self action:@selector(changeIcon)];
    [iconView addGestureRecognizer:tap];

    UILabel * nameLabel = [[UILabel alloc] init];
    CGFloat nameY = CGRectGetMaxY(iconView.frame) + 10;
    CGFloat nameW = KWidthSCREEN * 1/2;
    nameLabel.frame = CGRectMake(0, nameY, nameW, 20);
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.text = _myInfo.tel;
    nameLabel.textColor = [UIColor colorWithHexString:@"ffffff"];
    nameLabel.font = [UIFont systemFontOfSize:15];
    [headView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(0, nameY, nameW, 20);
    [loginBtn setTitle:@"未登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [headView addSubview:loginBtn];
    self.loginBtn = loginBtn;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 先判断有无存储账号信息
    if ([MGAccountTool getUsername] == nil) { // 之前没有登录成功
        self.loginBtn.hidden = NO;
        self.nameLabel.hidden = YES;
    } else {
        self.loginBtn.hidden = YES;
        self.nameLabel.hidden = NO;
    }
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 190;
}

- (void)loginBtnClick
{
    if ([_delegate respondsToSelector:@selector(HMSlideMenuDidClickLoginBtn:)]) {
        [_delegate HMSlideMenuDidClickLoginBtn:self];
    }
}

- (void)changeIcon
{
    if ([_delegate respondsToSelector:@selector(HMSlideMenu:clickIcon:)]) {
        [_delegate HMSlideMenu:self clickIcon:self.iconView];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
